package com.game.model.role;

import com.game.model.BaseProperty;
import com.game.model.SpecialProperty;

import java.io.*;
import java.util.Objects;

public abstract class Role implements Serializable {
    private int id;//编号
    private String name;//名称
    private int level;//等级
    private BaseProperty baseProperty;//基本属性
    private SpecialProperty specialProperty;//特殊属性
    private String description;//描述

    //序列化实现深拷贝，拷贝为了用来对数据进行处理使用新的类，而不改变当前的类
    public Role deepClone() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            oos.close();
            bis.close();
            return (Role) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Role() {
    }

    public Role(int id, String name, int level, BaseProperty baseProperty, SpecialProperty specialProperty, String description) {
        this.id = id;
        this.name = name;
        this.level = level;
        this.baseProperty = baseProperty;
        this.specialProperty = specialProperty;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void setBaseProperty(BaseProperty baseProperty) {
        this.baseProperty = baseProperty;
    }

    public void setSpecialProperty(SpecialProperty specialProperty) {
        this.specialProperty = specialProperty;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public BaseProperty getBaseProperty() {
        return baseProperty;
    }

    public SpecialProperty getSpecialProperty() {
        return specialProperty;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Role role = (Role) o;
        return id == role.id &&
                level == role.level &&
                Objects.equals(name, role.name) &&
                Objects.equals(description, role.description);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, level, description);
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", level=" + level +
                ", baseProperty=" + baseProperty +
                ", specialProperty=" + specialProperty +
                ", description='" + description + '\'' +
                '}';
    }
}
